<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=0.9,maximum-scale=0.9,user-scalable=yes" name="viewport">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<head>
	<title>吉他指板音游戏1.2</title>
	<style type="text/css">
	@media screen and (orientation:portrait) {}
	body{background-color:#f7f3f0;}
	div{font-size:20px;color:#333;}
	li{font-size:20px;list-style:none;}
	input{padding:5px;font-size:20px;color:#333;box-shadow:2px 2px 3px #ccc;border:solid 1px #aaa;border-radius:3px;}
	input:hover{background-color:#ccc}
	pre{margin:0;font-family:ya;}
	.panel{margin:10px 0 10px 40px;padding:10px;background-color:white;border:solid 1px #ccc;border-radius:5px;box-shadow:2px 2px 3px #ccc;}
	#container{width:1030px;}
	#main{height:300px;background-image:url(images/bg_blue.png);}
	#finger{width:26px;height:26px;line-height:26px;font-weight:bold;text-align:center;color:white;border:solid 1px #333;border-radius:50%;box-shadow:2px 2px 3px #333;position:absolute;}
	#s6{width:97px;height:81px;margin-left:-5px;background-image:url(images/6.png);float:left}
	#s6 div{width:84px;height:70px;margin:6px;background-color:gray;position:absolute;opacity:30%;}
	#s6 span{height:14px;line-height:14px;text-align:center;background-color:white;display:block;margin-left:40px;margin-top:0px;}
	#qsn{height:80px;}
	#qsn input{width:50px;}
	#kill{margin-top:-45px;font-size:35px;color:yellow;text-shadow:2px 2px 3px #666;-webkit-text-stroke:1px #666;text-stroke:1px #666;font-weight:bold;font-style:italic;position:absolute;right:320px;opacity:0}
	#right{color:green;}
	#wrong{color:red}
	#band{width:1030px;height:400px;background:url(images/band.png) no-repeat center;background-size:40%;opacity:20%;position:absolute}
	#record{height:400px;overflow-y:auto;}
	</style>
</head>
<body>
	<div id="container">
		<div id="main">
			<div id="finger"></div>
		</div>
		<div id="qsn" class="panel">
			<div id="s6"><div></div><span>0</span></div>
			<div id="qs"></div>
			<div id="op">
				<input type="button" value="1" onclick="choose(1);">
				<input type="button" value="2" onclick="choose(2);">
				<input type="button" value="3" onclick="choose(3);">
				<input type="button" value="4" onclick="choose(4);">
				<input type="button" value="5" onclick="choose(5);">
				<input type="button" value="6" onclick="choose(6);">
				<input type="button" value="7" onclick="choose(7);">
			</div>
			<div id="kill">0</div>
		</div>
		<div class="panel">
			<span id="setting">
				<input type="button" value="全部" data-value="all" onclick="partArr(this.getAttribute('data-value'));">
				|
				<input type="button" value="Mi型音阶" data-value="mi" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="La型音阶" data-value="la" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="Re型音阶" data-value="re" onclick="partArr(this.getAttribute('data-value'));">
				|
				<input type="button" value="1弦" data-value="1" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="2弦" data-value="2" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="3弦" data-value="3" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="4弦" data-value="4" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="5弦" data-value="5" onclick="partArr(this.getAttribute('data-value'));">
				<input type="button" value="6弦" data-value="6" onclick="partArr(this.getAttribute('data-value'));">
			</span>
			||
			<span id="settingBg">
				<input type="button" value="蓝色模式" data-value="blue" onclick="guitarBg(this.getAttribute('data-value'));">
				<input type="button" value="无色模式" data-value="no" onclick="guitarBg(this.getAttribute('data-value'));">
			</span>
		</div>
		<div id="score" class="panel">
			本次答对 <span id="right">0</span> 个 | 答错 <span id="wrong">0</span> 个 || 历史总共 <span id="all"></span> 个
		</div>
		<div id="cont" class="panel">
			<div id="band"></div>
			<div id="record"></div>
		</div>
	</div>
	<script type="text/javascript">
	/*********************/
	/*     游戏逻辑      */
	/*********************/
	//定义变量
	var finger = $('finger');
	var s6 = $('s6');
	var s6span = $('s6').getElementsByTagName('span')[0];
	var s6div = $('s6').getElementsByTagName('div')[0];
	var qs = $('qs');
	var op = $('op');
	var kill = $('kill');
	var arrOri = [
		[3,4,0,5,0,6,0,7,1,0,2,0,3,4],
		[7,1,0,2,0,3,4,0,5,0,6,0,7,1],
		[5,0,6,0,7,1,0,2,0,3,4,0,5,0],
		[2,0,3,4,0,5,0,6,0,7,1,0,2,0],
		[6,0,7,1,0,2,0,3,4,0,5,0,6,0],
		[3,4,0,5,0,6,0,7,1,0,2,0,3,4],
	];
	var arr = arrOri;
	var f,x,y,value,old,unlock,right=0,wrong=0,kills=0;
	var all = read('all') || 0;
	var bg = read('guitarBg') || 'blue';
	var beginTime;
	/**
	 *	初始化
	 */
	function init(){
		log(arr);
		//隐藏指板音和问题
		hide(finger);
		hide(s6);
		hide(qs);
		hide(op);
		
		//重置指板音
		finger.style.backgroundColor = 'blue';
		finger.innerText = '?';
		
		//生成问题
		answer();
		
		//设置记录
		$('all').innerText = all;
		
		//设置指板模式
		guitarBg(bg);
	}
	/**
	 *	生成答案并展示音符和问题
	 */
	function answer(){
		//生成答案
		f = rollFinger(arr);
		x = f.x;
		y = f.y;
		value = arr[x][y];
		finger.style.top = 46+38*x+'px';
		var offset = 0;
		if(y==0)offset = 20;
		if(y==13)offset = -20;
		finger.style.left = offset+32+76*y+'px';
		log('x:',x,', y:',y);
		log('音:',value);

		//展示音符
		blink(finger,3);
		
		//展示6线谱
		s6span.innerText = y;
		s6span.style.marginTop = 13*x+'px';
		if(y>9){
			s6span.style.width = 26+'px';
			s6span.style.marginLeft = 36+'px';
		}else{
			s6span.style.width = 14+'px';
			s6span.style.marginLeft = 42+'px';
		}
		
		//6线谱背景色
		if(bg=='no'){
			s6div.style.backgroundColor = 'transparent';
		}else if(bg=='blue'){
			if(y<4){
				s6div.style.backgroundColor = '#d8e0fa';
			}else if(y>=4 && y<9){
				s6div.style.backgroundColor = '#6b8dfb';
			}else if(y>=9){
				s6div.style.backgroundColor = '#003cff';
			}
		}
		
		//展示问题
		qs.innerText = (x+1)+' 弦 '+y+' 品是什么音?';
		setTimeout(function(){
			show(s6);
			show(qs);
			show(op);
			unlock = true;
			beginTime = new Date();
		},300);
	}
	
	/**
	 *	选择选项
	 */
	function choose(v){
	
		//选择后锁定
		if(!unlock) return;
		unlock = false;
		
		//判断对错,并显示到指板音颜色和记录上
		var rightFlag = false;
		var color = 'red';
		if(v==value){
			rightFlag = true;
			color = 'green';
			
			kill.innerText = ++kills+'连对!!';
			if(kills>1)
			zoomInAndOut(kill);
			$('right').innerText = ++right;
		}else{
			kills=0;
			$('wrong').innerText = ++wrong;
		}
		
		//显示指板音和颜色
		finger.style.backgroundColor = color;
		finger.innerText = value;
		
		//添加记录
		var curTime = new Date()
		var time = curTime.toLocaleString();
		var useTime = ((curTime.getTime() - beginTime.getTime())/1000).toFixed(1);
		$('record').innerHTML = '<pre>['+time+'] <span style="color:'+color+'">'+(x+1)+'弦'+y+'品&#9===> '+value+'</span>'+' (用时'+useTime+'秒)</pre>'+ $('record').innerHTML;
		$('all').innerText = ++all;
		save('all',all);

		var timer = 2000;
		if(!rightFlag)timer = 3000;
		setTimeout(function(){
			init();
		},timer);
	}
	
	/**
	 *	选择部分
	 */
	function partArr(_p){
		
		arr = [];
		
		if(!_p)_p='all';
		
		var regex = /\d/;
		if(regex.test(p) && regex.test(_p)){
			//没有加上
			if(p.indexOf(_p)==-1){
				p = p+','+_p;
				//排序
				p = p.split(',').sort().join(',');
			}
			//有了减去
			else{
				tmp = p.split(',').filter(item=>item!=_p).join(',');
				if(tmp){
					p=tmp;
				}else{
					return;
				}
			}			
		}else{
			p = _p;
		}
		save('partArr',p);
		
		var inputArr = $('setting').getElementsByTagName('input');
		for(var i=0;i<inputArr.length;i++){
			var input = inputArr[i];
			var dataValue = input.getAttribute('data-value');
			input.style.backgroundColor = '#f4f4f4';
			if(p.indexOf(dataValue)>-1)input.style.backgroundColor = '#7777ff';
		}
		
		if(p=='all'){
			arr = arrOri;
		}else{
			//填充空白数组,数组截取
			if(p=='mi'){
				cutArr(0,6,0,3);
			}else if(p=='la'){
				cutArr(0,6,4,8);
			}else if(p=='re'){
				cutArr(0,6,9,13);
			}else if(p>=1 || p<=6){
				fillArr(p);
				cutArr(p-1,p,0,13);
			}else if(p.indexOf(',')>-1){
				var pArr = p.split(',');
				var last = 0;
				for(var i=0;i<pArr.length;i++){
					var one = pArr[i];
					fillArr(one-last);
					cutArr(one-1,one,0,13);
					
					last = one;
				}
				log(p);
			}
		}
		
		init();
	}
	/**
	 *	更换吉他颜色
	 */
	function guitarBg(_bg){
		bg = _bg;
		var inputArr = $('settingBg').getElementsByTagName('input');
		for(var i=0;i<inputArr.length;i++){
			var input = inputArr[i];
			var dataValue = input.getAttribute('data-value');
			input.style.backgroundColor = '#f4f4f4';
			if(bg.indexOf(dataValue)>-1)input.style.backgroundColor = '#7777ff';
		}
		$('main').style.backgroundImage = 'url(images/bg_'+bg+'.png)';
		save('guitarBg',bg);
	}

	//初始化
	var p = read('partArr');
	partArr(p);

	//监听按键
	document.addEventListener('keydown', function(event) {
		var key = event.key;
		if(key>=1 && key<=7){
			choose(key);
		}
	});
	
	/*********************/
	/*     公用方法      */
	/*********************/
	function $(id){
		return document.getElementById(id);
	}
	function show(obj){
		obj.style.display = 'block';
	}
	function hide(obj){
		obj.style.display = 'none';
	}
	function blink(obj,count){
		var cnt = 1;
		var timer = setInterval(function(){
			if(cnt++%2==1){
				show(obj);
			}else{
				hide(obj);
			}
			if(cnt==count*2)clearInterval(timer);
		},100);
	}
	function zoomInAndOut(obj){
		var cnt = 5;
		var timer = setInterval(function(){
			obj.style.right = 320-cnt*20+'px';
			obj.style.opacity = (5-cnt)*0.2;
			if(cnt--==0){
				clearInterval(timer);
				setTimeout(function(){
					obj.style.opacity = 0;
				},1000);
				/*
				timer = setInterval(function(){
					obj.style.right = 320-cnt*20+'px';
					obj.style.opacity = (5-cnt)*0.2;
					if(cnt++==5)clearInterval(timer);
				},20);
				*/
			}
		},30);
	}
	function log(...s){
		console.log(...s);
	}
	function rollFinger(arr){
		var x = Math.floor(Math.random() * arr.length);
		var y = Math.floor(Math.random() * arr[0].length);
		var obj = {x:x,y:y};
		
		if(arr[x][y]==0 || JSON.stringify(obj)==JSON.stringify(old)){
			return rollFinger(arr);
		}else{
			old = obj;
			return obj;
		}
	}
	function fillArr(cnt){
		var arrBlank = [0,0,0,0,0,0,0,0,0,0,0,0,0,0];
		while(--cnt>0){
			arr.push(arrBlank);
		}
	}
	function cutArr(x0,x1,y0,y1){
		for(var i=x0;i<x1;i++){
			var arrTemp = [];
			for(var j=0;j<=y1;j++){
				arrTemp[j] = arrOri[i][j];
				if(j<y0)arrTemp[j] = 0;
			}
			arr.push(arrTemp);
		}	
	}
	function read(key){
		return localStorage.getItem(key);
	}
	function save(key,value){
		localStorage.setItem(key,value);
	}
	</script>
</body>
</html>



